<template>
  <div>
    <van-field label='标题' v-model='form.title'/>
    <van-field label='图片' v-model='form.pic'/>
    <van-field label='描述' v-model='form.descrip'/>
    选择讲师
    <select v-model='form.teacher_id'>
      <option :value="i.id" v-for='i in tlist'>{{ i.name }}</option>
    </select>



    <van-field name="radio" label="状态">
      <template #input>
        <van-radio-group v-model="form.status" direction="horizontal">
          <van-radio name="1">免费</van-radio>
          <van-radio name="2">收费</van-radio>
        </van-radio-group>
      </template>
    </van-field>
    选择分类
    <reply :data='cates'/>
        动态参数
        <div v-if='params'>
          <van-field v-for="(v,k) in params" :label='v' v-model="values[v]"/>
        </div>
    <van-button @click="add">添加</van-button>
  </div>
</template>

<script>

import bus from "./bus";
import reply from "./reply";


export default {
  name: "classM",
  data() {
    return {
      form: {},
      tlist: [],
      cates:{},
      params:{},
      values:{}
    }
  },
  components:{
    'reply':reply
  },
  methods: {
    //获取老师
    get_teacher() {
      this.$axios.get('users/teaM').then(res => {
        this.tlist = res.data.list
      })
    },
    getlist(){
      this.$axios.get('users/cateM').then(res=>{
        this.cates = {'id':0,'name':'333','son':res.data.list}
      })
    },
    add(){
      if(this.values){
        this.form.params = JSON.stringify(this.values)
      }
      this.$axios.post('users/courseM',this.form).then(res=>{
        if(res.data.code==200){
          alert('ok')
        }else {
          alert('no')
        }
      })
    }
  },
  mounted() {
    this.get_teacher()
    this.getlist()
    //监听
    bus.$on('pid',val=>{
      this.form.pid = val
      console.log(val)
      //通过pid查询分类params
      this.$axios.get('users/getCateByid?id='+val).then(res=>{
        if(res.data.params){
          this.params = JSON.parse(res.data.params)
        }else{
          this.params = {}
        }
      })
    })
  }
}
</script>

<style scoped>

</style>
